<template>
  <header class="header">
    <h1>todos</h1>
    <!-- 全选和取消全选 -->
    <input v-model="isAll" id="toggle-all" class="toggle-all" type="checkbox" />
    <label for="toggle-all"></label>
    <!-- 输入新增的任务名 -->
    <input
      v-model="name"
      @keyup.enter="addTodo"
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      name: "",
    };
  },
  methods: {
    addTodo() {
      if (!this.name) {
        return alert("任务名称不能为空！");
      }
      this.$emit("add-todo", this.name);
      this.name = "";
    },
  },
  computed: {
    // 全选
    // isAll() {
    //   return this.list.every(item=>item.isDone === true);
    // },
    isAll: {
      set(isChecked) {
        // console.log('全选框的选中状态', isChecked);
        return this.list.forEach((item) => {
          item.isDone = isChecked;
        });
      },
      get() {
        return this.list.every((item) => item.isDone === true);
      },
    },
  },
};
</script>